/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TASKTREEMODEL_H_
#define TASKTREEMODEL_H_

#include <QMap>
#include <QList>
#include <QAbstractItemModel>

#include <procycle/task.h>
#include "tasktreeitem.h"
#include "pcchild.h"


class TaskTreeModel : public QAbstractItemModel, public PCChild {

	Q_OBJECT

public:
	TaskTreeModel(PC::Cycle* cycle, ProCycle* parent = 0);
	virtual ~TaskTreeModel();

	void resetCycle(PC::Cycle* cycle);

	QVariant data(const QModelIndex& index, int role) const;

	Qt::ItemFlags flags(const QModelIndex &index) const;

	bool setData (const QModelIndex& index, const QVariant& value, int role=Qt::EditRole);

	QVariant headerData(int section, Qt::Orientation orientation,
			int role=Qt::DisplayRole) const;

	QModelIndex index(int row, int column,
			const QModelIndex &parent = QModelIndex()) const;

	QModelIndex parent(const QModelIndex &index) const;

	int rowCount(const QModelIndex &parent = QModelIndex()) const;

	bool hasChildren(const QModelIndex &parent) const;

	int columnCount(const QModelIndex &parent = QModelIndex()) const;

	bool hasColumn(PC::Task::Data col) const {
		return m_columns.contains(col);
	}
	PC::Task::Data column (int section) const;
	QList<PC::Task::Data> columns () const {
		return m_columns;
	}
	void setColumns(QList<PC::Task::Data> cols);


	TaskTreeItem* resolvItem (QString tbsId);
	QModelIndex resolvIndex(QString tbsId);
	QModelIndex indexForTask(const PC::Task* task);

	PC::Task* taskForIndex(const QModelIndex& index=QModelIndex());

signals:
	void columnsChanged();


public slots:

	void processTaskTimeChanged(PC::Task* task);
	void processTaskValueChanged(PC::Task* task, PC::Task::Data data);

	void processTaskCreated(PC::MacroTask* parent, PC::Task* task);
	void processTaskWillDelete(PC::Task* task);
	void processTaskTransformed(PC::MacroTask* parent, int taskPos);
	void processTaskMoved(PC::MacroTask* parentTo, PC::Task* parentFrom, int posTo, int posFrom);


private:

	TaskTreeItem*	m_root;

	QList<PC::Task::Data> m_columns;

};

#endif /* TASKTREEMODEL_H_ */
